﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using thousandclear.Data;
using thousandClear.Areas.Admin.Models;

namespace thousandClear.Areas.Admin.Controllers
{
    public class ConsumerController : BaseController
    {
        public ActionResult Index(int? page, int? count, string nickname,string cusnooo)
        {
            string level = "1";
            level = GetCookie("level");
            ViewBag.level = level;
            page = page ?? 1;
            count = count ?? 10;

            int totalCount = 0;
            int totalPage = 1;
            int currentPage = page ?? 1;
            int pageCount = count ?? 10;

            List<CustomerModel> list = new List<CustomerModel>();

            using (qianjingEntities db = new qianjingEntities())
            {
                var query = db.O_Customer.AsQueryable();

                if (!string.IsNullOrEmpty(nickname))
                {
                    query = query.Where(a => a.ocNicName.Contains(nickname));
                }

                if (!string.IsNullOrEmpty(cusnooo))
                {
                    var sso = db.O_Address.FirstOrDefault(o => o.oasNo.Contains(cusnooo));
                    if (sso != null)
                    {
                        query = query.Where(po => po.OCID == sso.oasOCID);
                    }
                    else
                    {
                        query = query.Where(iop => iop.OCID == -1);
                    }
                }

                totalCount = query.Count();

                List<O_Customer> records = query.OrderByDescending(a => a.OCID).Where(a=>a.RowStatus==1).Skip((currentPage - 1) * pageCount).Take(pageCount).ToList();

                foreach (var item in records)
                {
                    var ttmp = db.O_Address.FirstOrDefault(pp => pp.oasOCID == item.OCID);
                    string kehubianhao = "";

                    if (ttmp != null)
                    {
                        kehubianhao = ttmp.oasNo;
                    }
                    else
                    {
                        kehubianhao = "-";
                    }

                    decimal point = 0.00m;
                    if (item.ocGrades != null)
                    {
                        point = decimal.Parse(item.ocGrades.ToString());
                    }
                    list.Add(new CustomerModel
                    {
                        OCID = Convert.ToUInt16(item.OCID),
                        ocNicName = item.ocNicName,
                        CreatedDate = item.CreatedDate.ToString("yyyy-MM-dd HH:mm:ss"),
                        Point = point,
                        CreatedBy = kehubianhao
                    });
                }
            }

            totalPage = int.Parse((totalCount / pageCount).ToString()) + (totalCount % pageCount == 0 ? 0 : 1);

            ViewBag.TotalCount = totalCount;
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = currentPage;
            ViewBag.PageCount = pageCount;

            if (page <= 1)
            {
                ViewBag.PrevPageUrl = string.Format("../admin/consumer?page={0}&count={1}", page, count);
            }
            else
            {
                ViewBag.PrevPageUrl = string.Format("../admin/consumer?page={0}&count={1}", page - 1, count);
            }

            if (page >= totalPage)
            {
                ViewBag.NextPageUrl = string.Format("../admin/consumer?page={0}&count={1}", page, count);
            }
            else
            {
                ViewBag.NextPageUrl = string.Format("../admin/consumer?page={0}&count={1}", page + 1, count);
            }


            return View(list);
        }

        [HttpPost]
        public string UpdateCust(string ocid, string point)
        {
            using (var db = new qianjingEntities())
            {
                int so = int.Parse(ocid);
                var ttc = db.O_Customer.FirstOrDefault(oc => oc.OCID == so);
                decimal ocg = decimal.Parse(string.IsNullOrEmpty(point) ? "0" : point);
                ttc.ocGrades = ocg;
                db.SaveChanges();

            }
            return "1";
        }
    }
}